package com.solotiger.geographic.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.solotiger.geographic.biz.model.Picture;

import java.util.ArrayList;
import java.util.List;

public enum DatabaseManger {
    ME;
    private DatabaseHelper dbHelper;
    private SQLiteDatabase sqLiteDatabase;

    private List<Picture> collects;

    /**
     * 初始化,只一次
     *
     * @param context
     */
    public void init(Context context) {
        if (dbHelper == null) {
            dbHelper = new DatabaseHelper(context);
            sqLiteDatabase = dbHelper.getWritableDatabase();
            collects = new ArrayList<>();
            findAll();
        }
    }


    /**
     * 关闭数据库
     */
    public void close() {
        if (sqLiteDatabase.isOpen()) {
            sqLiteDatabase.close();
            sqLiteDatabase = null;
        }
        if (dbHelper != null) {
            dbHelper.close();
            dbHelper = null;
        }

    }

    /**
     * 保存
     */
    public void saveCollect(Picture picture) {
        if (sqLiteDatabase.isOpen()) {
            sqLiteDatabase.beginTransaction();
            try {
                collects.add(0, picture);
                ContentValues contentValues = new ContentValues();
                contentValues.put("id", picture.getId());
                contentValues.put("albumid", picture.getAlbumid());
                contentValues.put("title", picture.getTitle());
                contentValues.put("content", picture.getContent());
                contentValues.put("url", picture.getUrl());
                sqLiteDatabase.insert(DatabaseHelper.DATABASE_TABLE_NAME, null, contentValues);
                sqLiteDatabase.setTransactionSuccessful();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                sqLiteDatabase.endTransaction();
            }
        } else {
            throw new RuntimeException("数据库连接已关闭!");
        }
    }

    /**
     * 查询所有的收藏
     */
    public void findAll() {
        Cursor cursor = sqLiteDatabase.query(DatabaseHelper.DATABASE_TABLE_NAME, null, null, null, null, null, null);
        if (cursor.moveToFirst()) {
            do {
                Picture picture = new Picture();
                picture.setId(cursor.getString(cursor.getColumnIndex("id")));
                picture.setAlbumid(cursor.getString(cursor.getColumnIndex("albumid")));
                picture.setTitle(cursor.getString(cursor.getColumnIndex("title")));
                picture.setContent(cursor.getString(cursor.getColumnIndex("content")));
                picture.setUrl(cursor.getString(cursor.getColumnIndex("url")));
                collects.add(picture);
            } while (cursor.moveToNext());
        }
        cursor.close();
    }

    public void deleteItem(String id) {
        sqLiteDatabase.delete(DatabaseHelper.DATABASE_TABLE_NAME, "id=" + id, null);
        Picture tempItem = null;
        for (Picture pic : collects) {
            if (pic.getId().equals(id)) {
                tempItem = pic;
            }
        }
        if (tempItem != null) {
            collects.remove(tempItem);
        }
    }

    public boolean isCollect(Picture pic) {
        for (Picture item : collects) {
            if (item.getId().equals(pic.getId())) {
                return true;
            }
        }
        return false;
    }

    public List<Picture> getCollects() {
        return collects;
    }

}
